package com.xxkj.orcfx.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static final String YYYY_MM_DD_MM_HH_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDD_MM_HH_SS = "yyyy/MM/dd HH:mm:ss";
    public static final String YYYYMMDD2 = "yyyy/MM/dd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDDMMHHSSSSS = "yyyyMMddHHmmssSSS";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String MM_DD = "yyyyMMdd";
    public static final String YYYYMMDD_CN = "yyyy年MM月dd日";
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";

    public static final String[] DATE_FORMATS = {
            YYYYMMDD_MM_HH_SS,
            YYYY_MM_DD_MM_HH_SS,
            YYYYMMDD2,
            YYYY_MM_DD
    };

    public static Date toDate(String date){
        for(String dateFormat : DATE_FORMATS){
            try {
                return new SimpleDateFormat(dateFormat).parse(date);
            }catch (Exception e){}
        }
        return null;
    }
    
    public static Date toDate(String date, String format){
         try {
                return new SimpleDateFormat(format).parse(date);
        }catch (Exception e){}
        return null;
    }
    
    public static String toString(String date, String format, String toFormat){
        try {
        	Date d = toDate(date, format);
        	return toString(d, toFormat);
       }catch (Exception e){}
       return null;
   }

    public static String toString(Date date, String format){
        if(date==null || StringUtils.isEmpty(format))
            return null;
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.format(date);
    }

    public static String toString(Long date, String format){
        if(date==null || StringUtils.isEmpty(format))
            return null;
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.format(new Date(date));
    }

    public DateUtil() {
    }

    public static int daysBetween(Date smdate, Date bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            smdate = sdf.parse(sdf.format(smdate));
        } catch (ParseException var11) {
            var11.printStackTrace();
        }

        try {
            bdate = sdf.parse(sdf.format(bdate));
        } catch (ParseException var10) {
            var10.printStackTrace();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / 86400000L;
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static long diffDays(Date startDate, Date endDate) {
        long days = 0L;
        long start = startDate.getTime();
        long end = endDate.getTime();
        days = (end - start) / 86400000L;
        return days;
    }


    public static String getLastDayOfMonth(Date date, String dateFormat) {
        SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD);
        String startDay = df.format(date);
        Calendar calendar = Calendar.getInstance();
        //这里先设置要获取月份的下月的第一天
        calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)), 1);
        //这里将日期值减去一天，从而获取到要求的月份最后一天
        calendar.add(Calendar.DATE, -1);
        String lastDayOfMonth = new SimpleDateFormat( dateFormat).format(calendar.getTime());
        return lastDayOfMonth;
    }

    public static String getFirstDayOfMonth(Date date, String dateFormat) {
        SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD);
        String startDay = df.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.parseInt(startDay.substring(0,4)), Integer.parseInt(startDay.substring(5,7)) - 1, 1);
        String firstDayOfMonth = new SimpleDateFormat(dateFormat).format(calendar.getTime());
        return firstDayOfMonth;
    }

    public static Date addYear(int year) {
        return add(new Date(), 1, year);
    }

    public static Date addMonth(int month) {
        return add(new Date(), 2, month);
    }

    public static Date addDay(int day) {
        return add(new Date(), 6, day);
    }

    public static Date dateAddYear(Date date, int year) {
        return add(date, 1, year);
    }
    
    public static Date dateAddMonth(Date date, int month) {
        return add(date, 2, month);
    }
    
    public static Date dateAddDay(Date date, int day) {
        return add(date, 6, day);
    }
    
    public static String remainDateToString(Date startDate, Date endDate) {
        StringBuilder result = new StringBuilder();
        if (endDate == null) {
            return "过期";
        } else {
            long times = endDate.getTime() - startDate.getTime();
            if (times < -1L) {
                result.append("过期");
            } else {
                long temp = 86400000L;
                long d = times / temp;
                times %= temp;
                temp /= 24L;
                long m = times / temp;
                times %= temp;
                temp /= 60L;
                long s = times / temp;
                result.append(d);
                result.append("天");
                result.append(m);
                result.append("小时");
                result.append(s);
                result.append("分");
            }

            return result.toString();
        }
    }

    private static Date add(Date date, int type, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, value);
        return calendar.getTime();
    }

    public static String getLinkUrl(boolean flag, String content, String id) {
        if (flag) {
            content = "<a href='finance.do?id=" + id + "'>" + content + "</a>";
        }

        return content;
    }

    public static long getTimeCur(String format, String date) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.parse(sf.format(date)).getTime();
    }

    public static long getTimeCur(String format, Date date) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        return sf.parse(sf.format(date)).getTime();
    }

    public static String getStrTime(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        long lcc_time = Long.parseLong(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    public static int getAge(Date birthday) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(birthday);
        Calendar nowDate = Calendar.getInstance();
        return nowDate.get(1) - calendar.get(1);
    }

    public static String getDateMinusMinutes(int minutes) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowTime = new Date();
        String datetest = formatter.format(nowTime);
        Date date = formatter.parse(datetest);
        long Time1 = date.getTime() / 1000L - (long)(60 * minutes);
        date.setTime(Time1 * 1000L);
        return formatter.format(date);
    }

    public static boolean inValidTime(Date validTime, int time) {
        long currTime = System.currentTimeMillis();
        long valid = validTime.getTime();
        return currTime - valid < (long)(time * 1000);
    }

    public static int getYear(Date time) {
        if (time == null) {
            return -1;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            return calendar.get(1);
        }
    }

    public static int getMonth(Date time) {
        if (time == null) {
            return -1;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            return calendar.get(2) + 1;
        }
    }

    public static int getDay(Date time) {
        if (time == null) {
            return -1;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            return calendar.get(5);
        }
    }

    public static int getYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(1);
    }

    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(2) + 1;
    }

    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(5);
    }

    public static int getdiffMonth(Date start, Date end) {
        Calendar older = Calendar.getInstance();
        Calendar newer = Calendar.getInstance();
        older.setTime(end);
        newer.setTime(start);
        int diffMonth = 0;
        int year = older.get(1);
        int mount = older.get(2);
        int day = older.get(5);
        int nowyear = newer.get(1);
        int nowmount = newer.get(2);
        int nowday = newer.get(5);
       // int diffMonth;
        if (day > nowday) {
            diffMonth = (year - nowyear) * 12 + mount - nowmount;
        } else {
            diffMonth = (year - nowyear) * 12 + mount - nowmount - 1;
        }

        return diffMonth;
    }

    public static int getdiffMonthUp(Date start, Date end) {
        Calendar older = Calendar.getInstance();
        Calendar newer = Calendar.getInstance();
        older.setTime(end);
        newer.setTime(start);
        int diffMonth = 0;
        int year = older.get(1);
        int mount = older.get(2);
        int day = older.get(5);
        int nowyear = newer.get(1);
        int nowmount = newer.get(2);
        int nowday = newer.get(5);
        // int diffMonth;
        diffMonth = (year - nowyear) * 12 + mount - nowmount;

        return diffMonth;
    }

    public static String getDiffMonthDay(Date start, Date end) {
        int month = 0;
        int days = 0;

        try {
            month = getdiffMonth(start, end);
            Calendar cale = Calendar.getInstance();
            cale.setTime(start);
            cale.add(2, month);
            Date startTime = cale.getTime();
            days = daysBetween(startTime, end);
        } catch (Exception var6) {
            ;
        }

        return month == 0 && days == 0 ? "" : month + "月" + days + "天";
    }

    public static String yyyyMMddStrToMMDD(String date) {
        if (StringUtils.isEmpty(date)) {
            return date;
        } else if (!date.contains("-")) {
            return date;
        } else {
            String[] dates = date.split("-");
            return dates.length < 3 ? date : dates[1] + "-" + dates[2];
        }
    }

    public static boolean compare(String DATE1, String DATE2, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);

        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            return dt1.getTime() > dt2.getTime();
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(DateUtil.getFirstDayOfMonth(new Date(), DateUtil.YYYYMMDD2));
        System.out.println(DateUtil.getLastDayOfMonth(new Date(), DateUtil.YYYYMMDD2));
    }
}
